package com.hub.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.hub.service.impl.BaseServiceImpl;
import com.hub.dto.asset.CategoryInfoDto;
import com.hub.dto.asset.SearchCategoryInfoDto;
import com.hub.entity.asset.AssetCategoryInfo;
import com.hub.frame.consts.ConstantUtil;
import com.hub.utils.ApiCodeEnum;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.mapper.asset.CategoryInfoMapper;
import com.hub.model.asset.CategoryInfoModel;
import com.hub.service.asset.ICategoryInfoService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryInfoServiceImpl extends BaseServiceImpl<CategoryInfoMapper, AssetCategoryInfo> implements ICategoryInfoService {
    @Autowired
    private CategoryInfoMapper categoryInfoMapper;
    @Autowired
    private Mapper mapper;

    /**
     * 
     * 分页查询
     * @param queryModel
     * @return
     */
    public PagingResultObject getList(SearchCategoryInfoDto queryModel){
        queryModel.setPageIndex( queryModel.getPageIndex() > 0 ? queryModel.getPageIndex() : 1);
        queryModel.setPageSize(queryModel.getPageSize() > 0?queryModel.getPageSize():20);
        //查询条件
        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(queryModel.getCategoryCode())){
            wrapper.like("category_code", queryModel.getCategoryName());
        }
        if(StringUtil.isNotEmpty(queryModel.getCategoryName())){
            wrapper.like("category_name",queryModel.getCategoryName());
        }
        wrapper.eq("active_flg", ConstantUtil.YES);
        IPage<AssetCategoryInfo> result = baseMapper.selectPage(new Page<>(queryModel.getPageIndex(), queryModel.getPageSize()), wrapper);
        List<CategoryInfoModel> modelList = new ArrayList<>();
        for(AssetCategoryInfo entity : result.getRecords()){
            CategoryInfoModel model = mapper.map(entity,CategoryInfoModel.class);
            //判断是否有父类
            if(entity.getParentCode().equals("0")){
                model.setParentName("所有类型");
            }else{
                //获取父类的名称
                String parentCategoryName = this.categoryInfoMapper.getCategoryNameByCode(entity.getParentCode());
                model.setParentName(parentCategoryName);
            }
            modelList.add(model);
        }
        return PagingResultObject.success(modelList,queryModel.getPageIndex(),queryModel.getPageSize(),result.getTotal());
//        //分页
//        Page page = PageHelper.offsetPage(queryModel.getPageIndex(), queryModel.getPageSize(),true);
//        //分页查询
//        List<CategoryInfoModel> categoryList = this.categoryInfoMapper.selectByPage(queryModel);
//        return PagingResultObject.success(categoryList,queryModel.getPageIndex(),queryModel.getPageSize(),page.getTotal());
    }

    /**
     * 
     * 获取所有分类
     * @return
     */
    public ApiResultObject getAllList(){
        Wrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        //获取所有分类
        List<AssetCategoryInfo> allList = baseMapper.selectList(wrapper);
        Map<String,String> categoryMap = new HashMap<String,String>();
        List<CategoryInfoModel> result = new ArrayList<>();
        for(AssetCategoryInfo item : allList){
            CategoryInfoModel model = mapper.map(item,CategoryInfoModel.class);
            result.add(model);
        }
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"",result);
    }

    /**
     * 获取所有一级分类信息
     * @return
     */
    public ApiResultObject getAllMainCategory() {

        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code","0");
        wrapper.eq("active_flg", ConstantUtil.YES);
        //获取所有分类
        List<AssetCategoryInfo> allList = baseMapper.selectList(wrapper);

        List<CategoryInfoModel> result = new ArrayList<>();
        for(AssetCategoryInfo item : allList){
            CategoryInfoModel model = mapper.map(item,CategoryInfoModel.class);
            result.add(model);
        }
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"",result);

    }

    /**
     * 获取一级分类信息
     * @return
     */
    public AssetCategoryInfo getMainCategoryByCode(String code) {
        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code","0")
                .eq("category_code",code)
                .eq("active_flg", ConstantUtil.YES);

        return baseMapper.selectOne(wrapper);
        //CategoryInfoModel model = mapper.map(entity,CategoryInfoModel.class);
        //return ApiResultObject.success(model);
    }

    /**
     * 根据父分类及分类Code获取分类信息
     * @return
     */
    public AssetCategoryInfo getCategoryByParentCodeAndCode(String parentCode ,String code) {
        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code",parentCode)
                .eq("category_code",code)
                .eq("active_flg", ConstantUtil.YES);

        return baseMapper.selectOne(wrapper);
        //CategoryInfoModel model = mapper.map(entity,CategoryInfoModel.class);
        //return ApiResultObject.success(model);
    }

    /**
     * 根据父分类编号，获取所有的子分类
     * @param code
     * @return
     */
    public ApiResultObject getCategoryByParentCode(String code) {

        if(StringUtil.isEmpty(code))
            return ApiResultObject.failed("参数异常: 查询父分类编码不能是空!");

        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code",code);
        wrapper.eq("active_flg", ConstantUtil.YES);
        //获取所有分类
        List<AssetCategoryInfo> allList = baseMapper.selectList(wrapper);

        List<CategoryInfoModel> result = new ArrayList<>();
        for(AssetCategoryInfo item : allList){
            CategoryInfoModel model = mapper.map(item,CategoryInfoModel.class);
            result.add(model);
        }
        return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"",result);
    }

    /**
     * 
     * 新增分类
     * @param addModel
     * @return
     */
    public ApiResultObject addAssetCategory(CategoryInfoDto addModel){
        //检验分类信息是否存在
        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_code",addModel.getCategoryCode() ).or().eq("category_name",addModel.getCategoryName());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("分类名或编码已存在，无法保存,请重新输入！");
        }
        //新增
        AssetCategoryInfo entity = mapper.map(addModel,AssetCategoryInfo.class);
        if(this.save(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"添加成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"添加失败！");
    }

    /**
     * 
     * 修改分类
     * @param modifyModel
     * @return
     */
    public ApiResultObject modifyAssetCategory(CategoryInfoDto modifyModel){
        //验证分类编码及分类名是否已经存在
        QueryWrapper<AssetCategoryInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_code",modifyModel.getCategoryCode()).or().eq("category_name",modifyModel.getCategoryName());
        wrapper.ne("id",modifyModel.getId());
        wrapper.eq("active_flg",ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("分类编码或分类名称已存在，无法保存,请重新输入！");
        }
        //修改
        AssetCategoryInfo entity = mapper.map(modifyModel,AssetCategoryInfo.class);
        if(this.updateById(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"修改成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"修改失败！");
    }

    /**
     * 
     * 删除分类
     * @param id
     * @return
     */
    public ApiResultObject deleteAssetCategory(int id){
        if(id < 0){
            return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"参数异常: 资产信息ID异常!");
        }
        int count = this.categoryInfoMapper.getIfExistSubCategory(id);
        if(count > 0){
            return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"此分类下含有子级分类，无法删除！");
        }
        if(this.removeById(id)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"删除成功！");
        }
        return ApiResultObject.success(ApiCodeEnum.FAILED.getCode(),"删除失败！");
    }
}
